package com.example.testdemo.kstatic.sprite;

import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import com.example.testdemo.manager.ResourcesManager;
import com.example.testdemo.pools.ShieldPools;
import com.example.testdemo.resources.CharacterResources;

public class ShieldSprite extends AnimatedSprite {

	@SuppressWarnings("unused")
	private final String TAG = ShieldSprite.class.getSimpleName();

	private ShieldPools mPools = null;
	private CharacterResources mResources = null;

	public ShieldSprite(ShieldPools pools, float pX, float pY,
	        ITiledTextureRegion pTiledTextureRegion,
	        VertexBufferObjectManager pVertexBufferObjectManager) {
		super(pX, pY, pTiledTextureRegion, pVertexBufferObjectManager);
		setCullingEnabled(true);
		mPools = pools;
		mResources = (CharacterResources)ResourcesManager.getInstance().getResources(ResourcesManager.RESOURCES_CHARACTER);
	}

	private IAnimationListener mAnimationListener = new IAnimationListener() {

		@Override
		public void onAnimationStarted(AnimatedSprite pAnimatedSprite,
		        int pInitialLoopCount) {
		}

		@Override
		public void onAnimationFrameChanged(AnimatedSprite pAnimatedSprite,
		        int pOldFrameIndex, int pNewFrameIndex) {
		}

		@Override
		public void onAnimationLoopFinished(AnimatedSprite pAnimatedSprite,
		        int pRemainingLoopCount, int pInitialLoopCount) {
		}

		@Override
		public void onAnimationFinished(AnimatedSprite pAnimatedSprite) {
			mPools.recycle(ShieldSprite.this);
		}

	};
	
	public void soud() {
//		mResources.mSoundShield.play();
	}

	public void animate(Sprite attachTo, long pFrameDurationEach) {
		float widDiff = getWidth() - attachTo.getWidth();
		float heiDiff = getHeight() - attachTo.getHeight();
		float x = attachTo.getX() - widDiff / 2;
		float y = attachTo.getY() - heiDiff / 2;
		setPosition(x, y);
		setVisible(true);
		super.animate(pFrameDurationEach, false, mAnimationListener);
	}
}
